package com.music.controller.music;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.music.dto.music.PlaylistDTO;
import com.music.dto.music.SongDTO;
import com.music.dto.system.TokenDTO;
import com.music.dto.utils.OpResultDTO;
import com.music.dto.utils.TableReqDTO;
import com.music.dto.utils.TableRspDTO;
import com.music.service.music.PlaylistService;
import com.music.service.music.SongService;
import com.music.utils.HttpRequestReader;
import com.music.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/song")
public class SongController {
    @Resource
    private SongService songService;
    private final Logger logger = LoggerFactory.getLogger(SongController.class);

    /**
     * @Description: 获取数据列表
     * @Author dy
     * @Date 2021/11/26
     */
    @RequestMapping(value = "/queryByPlaylistId", method = RequestMethod.POST)
    public TableRspDTO queryByPlaylistId(@RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = songService.queryByPlaylistId(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 新增记录
     * @Author dy
     * @Date 2021/11/26
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestBody SongDTO songDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(songService.add(songDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 更新记录
     * @Author dy
     * @Date 2021/11/26
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody SongDTO songDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(songService.update(songDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 删除记录
     * @Author dy
     * @Date 2021/11/26
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) throws Exception {
        OpResultDTO opResult = new OpResultDTO();
        JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
        String songIds = jsonObject.getString("songIds");
        try {
            opResult.setMessage(songService.remove(songIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 获取音乐广场数据列表
     * @Author hbh
     * @Date 2021/11/27
     */
    @RequestMapping(value = "/queryDetailByPlaylistId", method = RequestMethod.POST)
    public TableRspDTO queryDetailByPlaylistId(@RequestHeader(name = "Access-Token") String token,
                                               @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        TokenDTO tokenDTO = JWTUtil.verifyToken(token);
        try {
            tableRspDTO = songService.queryDetailByPlaylistId(tableReqDTO, tokenDTO.getAccountId());
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }
}
